<!doctype html>



  


<html class="theme-next pisces use-motion">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />












  
  
  <link href="/vendors/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/vendors/font-awesome/css/font-awesome.min.css?v=4.4.0" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.0.1" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="王雨峰, 博客" />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.0.1" />






<meta name="description" content="原文发表于2014-10-25
&amp;nbsp;
0.本来十分不解，不能理解形如
int (*p)[100];这样的东西。
&amp;nbsp;
1.
百度了一下，大概理解数组指针如果用来处理二维数组，指向的是每”行“的第零个元素的地址。
比如有：
int array[100];int (*p)[100];p = array;//第二行的100是个提示，提示你一行有100个元素那么有
(p)  //array">
<meta property="og:type" content="article">
<meta property="og:title" content="对“数组指针”的理解">
<meta property="og:url" content="http://wangyufeng.org/2015/03/28/e5-af-b9-e6-95-b0-e7-bb-84-e6-8c-87-e9-92-88-e7-9a-84-e7-90-86-e8-a7-a3/index.html">
<meta property="og:site_name" content="王雨峰的博客">
<meta property="og:description" content="原文发表于2014-10-25
&amp;nbsp;
0.本来十分不解，不能理解形如
int (*p)[100];这样的东西。
&amp;nbsp;
1.
百度了一下，大概理解数组指针如果用来处理二维数组，指向的是每”行“的第零个元素的地址。
比如有：
int array[100];int (*p)[100];p = array;//第二行的100是个提示，提示你一行有100个元素那么有
(p)  //array">
<meta property="og:updated_time" content="2015-07-27T08:02:39.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="对“数组指针”的理解">
<meta name="twitter:description" content="原文发表于2014-10-25
&amp;nbsp;
0.本来十分不解，不能理解形如
int (*p)[100];这样的东西。
&amp;nbsp;
1.
百度了一下，大概理解数组指针如果用来处理二维数组，指向的是每”行“的第零个元素的地址。
比如有：
int array[100];int (*p)[100];p = array;//第二行的100是个提示，提示你一行有100个元素那么有
(p)  //array">



<script type="text/javascript" id="hexo.configuration">
  var NexT = window.NexT || {};
  var CONFIG = {
    scheme: 'Pisces',
    sidebar: {"position":"left","display":"post"},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: 0,
      author: '博主'
    }
  };
</script>




  <link rel="canonical" href="http://wangyufeng.org/2015/03/28/e5-af-b9-e6-95-b0-e7-bb-84-e6-8c-87-e9-92-88-e7-9a-84-e7-90-86-e8-a7-a3/"/>

  <title> 对“数组指针”的理解 | 王雨峰的博客 </title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  










  
  
    
  

  <div class="container one-collumn sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-meta ">
  

  <div class="custom-logo-site-title">
    <a href="/"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">王雨峰的博客</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
  <p class="site-subtitle">随便写点东西，为互联网贡献一些比特~</p>
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      

      
    </ul>
  

  
</nav>

 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  
  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                对“数组指针”的理解
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            <span class="post-meta-item-icon">
              <i class="fa fa-calendar-o"></i>
            </span>
            <span class="post-meta-item-text">发表于</span>
            <time itemprop="dateCreated" datetime="2015-03-28T15:18:03+08:00" content="2015-03-28">
              2015-03-28
            </time>
          </span>

          
            <span class="post-category" >
              &nbsp; | &nbsp;
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
              
                <span itemprop="about" itemscope itemtype="https://schema.org/Thing">
                  <a href="/categories/代码/" itemprop="url" rel="index">
                    <span itemprop="name">代码</span>
                  </a>
                </span>

                
                

              
            </span>
          

          
            
          

          

          
          

          
        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>原文发表于<span class="gray">2014-10-25</span></p>
<p>&nbsp;</p>
<p>0.本来十分不解，不能理解形如</p>
<p><pre class="cpp">int (*p)[100];</pre><br><span style="font-family: Courier New; background-color: #f0f0f0;">这样的东西。</span></p>
<p>&nbsp;</p>
<p><span style="font-family: Courier New; background-color: #f0f0f0;">1.</span></p>
<p>百度了一下，大概理解数组指针如果用来处理二维数组，指向的是每”行“的第零个元素的地址。</p>
<p>比如有：</p>
<p><pre class="cpp">int array[100];<br>int (*p)[100];<br>p = array;<br>//第二行的100是个提示，提示你一行有100个元素</pre><br>那么有</p>
<p><pre class="cpp"><em>(p)  //array[0]，即第零行第零个元素的地址
</em>(p + i)  //array[i]<br><em>(p + i) + j    //array[i][j]的地址
</em>(*(p + i) + j) //array[i][j]的值</pre><br>&nbsp;</p>
<p>2.</p>
<p>程序L11-1，功能是输入若干字符串，找出其中最长的一句</p>
<p>原来用text[0]的实现：</p>
<p><pre class="cpp">//L11-1</pre></p>
<p>#include &lt;stdio.h&gt;</p>
<p>#include &lt;string.h&gt;</p>
<p>int main()<br>{<br>    char text[100][100] = {0};<br>    printf(“输入一些文本：\n”);</p>
<pre><code>//输入文本
int i = 0;
while(1)
{
    gets(text[i]);

    if (0 == strlen(text[i]))
    {
        break;
    }
    i++;
}

//处理
int j = 0;
char* max = text[j];
for (j = 0; j &amp;lt; i; j++)
{
    if ( strlen(max) &amp;lt; strlen(text[j+1]) )
    {
        max = text[j+1];
    }
}

printf(&quot;最长的行是%s&quot;,max);

return 0;
</code></pre><p>}<br><br>改用数组指针后：</p>
<p><pre class="cpp">//L11-1</pre></p>
<p>#include &lt;stdio.h&gt;</p>
<p>#include &lt;string.h&gt;</p>
<p>#include &lt;assert.h&gt;</p>
<p>int main()<br>{<br>    char text[100][100] = {0};<br>    printf(“输入一些文本：\n”);</p>
<pre><code>char (*p)[100];
p = text;

//输入文本
int i = 0;
while(1)
{
    gets(*(p+i));

    if (!strlen(*(p+i)))
    {
        break;
    }
    i++;
}

//处理
int j = 0;

char * max = *p;
//char (*max)[100] = text;

for (j = 0; j &amp;lt; i; j++)
{
    if ( strlen(max) &amp;lt; strlen(*(p+j)) )
    {
        max = *(p+j);
    }
}

printf(&quot;最长的行是%s&quot;,max);

return 0;
</code></pre><p>}<br><br>3.</p>
<p>此程序中有等价关系：</p>
<p><pre class="cpp">int text[20][100] = {0};</pre></p>
<p>int <em> p = text[0];<br>//等价于<br>int (</em>p)[100] = text;  //赋值关系是p = text;<br>&nbsp;</p>
<hr>
<p>4.仍然有疑问：</p>
<p>这种所谓“指向数组的指针”和普通的指向一维数组的普通指针有何区别？</p>
<p><pre class="cpp">int array1[10] = {0};<br>int * p1 = array1;</pre></p>
<p>int array2[10][10] = {0};<br>int (*p2)[10] = array2;</p>
<p>//这有区别？=.=<br>&nbsp;</p>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <footer class="post-footer">
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2015/03/28/e8-b0-a2-e5-b0-94-e5-ae-be-e6-96-af-e5-9f-ba-e4-b8-89-e8-a7-92-e5-bd-a2-ef-bc-8c-e6-b7-b7-e6-b2-8c-e6-b8-b8-e6-88-8f-e5-ae-9e-e7-8e-b0/" rel="next" title="谢尔宾斯基三角形，“混沌游戏”实现">
                <i class="fa fa-chevron-left"></i> 谢尔宾斯基三角形，“混沌游戏”实现
              </a>
            
          </div>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2015/03/28/e5-b0-86c-e8-af-ad-e8-af-be-e8-ae-be-e4-bc-a0-e5-88-b0-e4-ba-86github-e5-92-8ccode-e4-b8-8a/" rel="prev" title="将C语课设传到了Github和Code上">
                将C语课设传到了Github和Code上 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel ">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/images/avatar.gif"
               alt="Alan" />
          <p class="site-author-name" itemprop="name">Alan</p>
          <p class="site-description motion-element" itemprop="description">Alan Wang's personal blog.</p>
        </div>
        <nav class="site-state motion-element">
          <div class="site-state-item site-state-posts">
            <a href="/archives">
              <span class="site-state-item-count">57</span>
              <span class="site-state-item-name">日志</span>
            </a>
          </div>

          
            <div class="site-state-item site-state-categories">
              
                <span class="site-state-item-count">9</span>
                <span class="site-state-item-name">分类</span>
              
            </div>
          

          
            <div class="site-state-item site-state-tags">
              
                <span class="site-state-item-count">4</span>
                <span class="site-state-item-name">标签</span>
              
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
        </div>

        
        

        
        

      </section>

      
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">
            
              
            
            
              <p class="post-toc-empty">此文章未包含目录</p>
            
          </div>
        </section>
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2016</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Alan</span>
</div>

<div class="powered-by">
  Powered by <a class="theme-link" href="https://hexo.io">Hexo</a> 
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Pisces
  </a>
</div>

        

        
      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  



  
  <script type="text/javascript" src="/vendors/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/vendors/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/vendors/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/vendors/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/vendors/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/vendors/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.0.1"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.0.1"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.0.1"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.0.1"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.0.1"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.0.1"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.0.1"></script>



  



  




  
  

  

  

  

</body>
</html>
